﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace DSS_Simplex
{
    public partial class Form2 : Form
    {
        string MyConnectionString = "Server=localhost;Database=dss;Uid=root;Pwd=;";
        private string textFilter;
        private DataSet ds;
        Form3 formSearch;

        public Form2()
        {
            InitializeComponent();
            BindDatakeGrid();
        }
        private void BindDatakeGrid() 
        {
            MySqlConnection connection = new MySqlConnection(MyConnectionString);
            connection.Open();
            try
            {
                MySqlCommand cmd = connection.CreateCommand();
                cmd.CommandText = "SELECT * FROM variable";
                MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                da.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
           
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format(ex.Message),"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (formSearch == null)
            {  
                List<field> fields = new List<field>();
                for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                {
                    if (ds.Tables[0].Columns[i].DataType == typeof(string))
                    {
                        field f = new field();
                        f.Field = ds.Tables[0].Columns[i].ColumnName;
                        f.FriendlyName = f.Field;
                        fields.Add(f);
                    }
                }
                formSearch = new Form3(fields);
                formSearch.searchgrid_TextChanged += new SearchContextChangedHandler(searchgrid_TextChanged); 
            }
            formSearch.ShowDialog();
            
         }

        private void searchgrid_TextChanged(List<field> fields)
        {
            System.Data.DataView dv = ((System.Data.DataView)dataGridView1.DataSource);
            textFilter = ""; //reset
            bool first = true;
            foreach (field f in fields) 
            {
                if (f.Value.Length > 0) 
                {
                    if (!first) textFilter += "and";
                    textFilter += f.Field + "like '%" + f.Value + "%'";
                    first = false;
                }
            }
            dv.RowFilter = textFilter;
            this.label1.Text = textFilter;
        }

       private void button1_Click(object sender, EventArgs e)
        {
            formSearch = null;
            textFilter = "";
            this.label1.Text = "Filter: None";
            System.Data.DataView dv = ((System.Data.DataView)dataGridView1.DataSource);
            dv.RowFilter = textFilter;
        }
    }
}